package com.heima.wemedia.config;

import com.heima.common.constants.message.PublishArticleConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DeclarePubArticleRabbitConfig {

    /**
     * 延时交换机
     * RabbitMQ需要安装延迟插件
     */
    @Bean
    DirectExchange delayExchange(){
        return ExchangeBuilder.directExchange(PublishArticleConstants.DELAY_DIRECT_EXCHANGE)
                .delayed()
                .build();
    }

    /**
     * 声明发布文章队列
     * @return
     */
    @Bean
    Queue delayQueue(){
        return new Queue(PublishArticleConstants.PUBLISH_ARTICLE_QUEUE);
    }

    /**
     * 绑定 延迟交换机 + 发布文章队列
     * @return
     */
    @Bean
    Binding binding(){
        return BindingBuilder.bind(delayQueue())
                .to(delayExchange())
                .with(PublishArticleConstants.PUBLISH_ARTICLE_ROUTE_KEY);
    }
}
